#!/usr/bin/perl

######################################################################
package Swignition::GenericParser::OpenCalaisML;
######################################################################

use XML::LibXML qw(:all);

sub is_enabled
{
	my $this = shift;
	return ($this->uri =~ m#^http://d.opencalais.com/#);
}

sub consume
{
	my $this = shift;
	
	my $Concept;
	if ($this->uri =~ /\.html/)
	{
		my $u = $this->uri;
		$u =~ s/\.html$//;
		$Concept = Swignition::RDFModel::Subject->new($u);
	}
	else
	{
		$Concept = Swignition::RDFModel::Subject->new(
			Swignition::GenericParser::Utils::BNodeURI(undef, 'Concept'));
	}
	
	my $Page    = Swignition::RDFModel::Subject->new($this->uri);
	$Page->addVal('http://xmlns.com/foaf/0.1/primaryTopic',
		Swignition::RDFModel::Value->new($Concept->uri, 1));
	
	my @dataItems = $this->{'DOM'}->getElementsByTagName('li');
	foreach my $element (@dataItems)
	{
		my $rtype = $element->getAttribute('rtype');
		my $nsuri = $element->getAttribute('nsuri');
	
		next unless (length $rtype && length $nsuri);
		
		my ($rpfx, $rsfx) = split /:/, $rtype;
		
		my $target;
		my @anchors = $element->getElementsByTagName('a');
		$target = $this->uri( $anchors[0]->getAttribute('href') )
			if (@anchors);
		
		if (length $target)
		{
			$Concept->addVal($nsuri . $rsfx,
				Swignition::RDFModel::Value->new($target, 1));
		}
		else
		{
			my $string = Swignition::GenericParser::Utils::STRINGIFY($element);
			$Concept->addVal($nsuri . $rsfx,
				Swignition::RDFModel::Value->new($string));
		}
		
		$this->mergeNS($rpfx, $nsuri, "The $rpfx Namespace", $element, 0)
	}
	
	$this->mergeSubject($Page);
	$this->mergeSubject($Concept);
}


1;
